package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.feedback.Feedback;
import com.usian.game.GameLogs;
import com.usian.mapper.SystemMapper;
import com.usian.role.Role;
import com.usian.room.Room;
import com.usian.service.SystemService;
import com.usian.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 米西米西
 */
@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    private SystemMapper systemMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<Feedback> getFeedback(Integer pageNum, Integer pageSize, Feedback feedback) {
        PageHelper.startPage(pageNum,pageSize);
        List<Feedback> feedbackList = systemMapper.getFeedback(feedback);

        feedbackList.forEach(feedback1 -> {
            Integer userId = feedback1.getUserId();
            String userName = systemMapper.findUserName(userId);
            feedback1.setUserName(userName);
        });

        return new PageInfo<>(feedbackList);
    }

    @Override
    public Integer updateUser(User user) {
        return systemMapper.updateUser(user);
    }

    @Override
    public List<GameLogs> getGameUserLogs(Integer roomid) {
        return systemMapper.getGameUserLogs(roomid);
    }

    @Override
    public Integer getRoomUser(Integer roomid) {
        return systemMapper.getRoomUser(roomid);
    }

    @Override
    public void delRoomUser(Integer roomid, Integer userId) {
//        删除玩家在房间内信息
        redisTemplate.delete(roomid+"roomUser");
//        删除数据库房间内玩家信息
        systemMapper.delRoomUsers(roomid,userId);
    }

    @Override
    public PageInfo<Room> findRoom(Integer pageNum, Integer pageSize, Room room) {
        PageHelper.startPage(pageNum,pageSize);

        List<Room> room1 = systemMapper.findRoom(room);

//        判断房主id找到对应的房主姓名
        for (Room room2 : room1) {
            Integer roomUser = room2.getRoomUser();
            String roomName = systemMapper.findUserName(roomUser);
            room2.setUserName(roomName);
        }

        PageInfo<Room> pageInfo = new PageInfo<>(room1);

        return pageInfo;
    }

    @Override
    public List<User> userFindAll(User user) {
        return systemMapper.userFindAll(user);
    }

    @Override
    public PageInfo<Role> findRolePage(Integer pageNum, Integer pageSize, Role role) {
        PageHelper.startPage(pageNum,pageSize);
        List<Role> roleList = systemMapper.findRolePage(role);
        return new PageInfo<>(roleList);
    }

    @Override
    public PageInfo<User> findUserPage(Integer pageNum, Integer pageSize, User user) {
        PageHelper.startPage(pageNum,pageSize);

        return new PageInfo<>(systemMapper.userFindAll(user));

    }

    @Override
    public List<User> userFindUserId(Integer id) {
        return systemMapper.userFindUserId(id);
    }

}
